using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for AuthenticationScriptControl.
	/// </summary>
	public class AuthenticationScriptControl : AdminUserControl
	{
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox nameLabel;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.TextBox scriptTextBox;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Panel panel1;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public AuthenticationScriptControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Apply changes", "Apply any changes made", new ActionExecuteDelegate(ApplyChanges));
      actionList.Add("Delete", "Delete this script", new ActionExecuteDelegate(DeleteScript));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AuthenticationScriptControl));
      this.label1 = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.TextBox();
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.scriptTextBox = new System.Windows.Forms.TextBox();
      this.label2 = new System.Windows.Forms.Label();
      this.panel1 = new System.Windows.Forms.Panel();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
      this.panel1.SuspendLayout();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(40, 16);
      this.label1.TabIndex = 0;
      this.label1.Text = "Name:";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.nameLabel.Location = new System.Drawing.Point(48, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.ReadOnly = true;
      this.nameLabel.Size = new System.Drawing.Size(490, 13);
      this.nameLabel.TabIndex = 1;
      this.nameLabel.Text = "nameLabel";
      // 
      // pictureBox1
      // 
      this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(546, 8);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 32);
      this.pictureBox1.TabIndex = 2;
      this.pictureBox1.TabStop = false;
      // 
      // scriptTextBox
      // 
      this.scriptTextBox.AcceptsTab = true;
      this.scriptTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
      this.scriptTextBox.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.scriptTextBox.Location = new System.Drawing.Point(0, 48);
      this.scriptTextBox.Multiline = true;
      this.scriptTextBox.Name = "scriptTextBox";
      this.scriptTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
      this.scriptTextBox.Size = new System.Drawing.Size(586, 304);
      this.scriptTextBox.TabIndex = 3;
      this.scriptTextBox.Text = "scriptTextBox";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 32);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(100, 16);
      this.label2.TabIndex = 4;
      this.label2.Text = "Script:";
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.label1);
      this.panel1.Controls.Add(this.pictureBox1);
      this.panel1.Controls.Add(this.nameLabel);
      this.panel1.Controls.Add(this.label2);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(586, 48);
      this.panel1.TabIndex = 5;
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(586, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 352);
      this.uiActionsControl.TabIndex = 6;
      // 
      // AuthenticationScriptControl
      // 
      this.Controls.Add(this.scriptTextBox);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "AuthenticationScriptControl";
      this.Size = new System.Drawing.Size(736, 352);
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
      this.panel1.ResumeLayout(false);
      this.panel1.PerformLayout();
      this.ResumeLayout(false);
      this.PerformLayout();

    }
		#endregion

    private AuthenticationScript script;
    public void LoadScript(AuthenticationScript script)
    {
      this.script = script;
      nameLabel.Text = script.Name;
      scriptTextBox.Text = script.Script;
    }

    private string ApplyChanges()
    {
      script.Script = scriptTextBox.Text;
      script.ApplyChanges();
      return Constants.ChangesApplied;
    }

    private string DeleteScript()
    {
      if (MessageBox.Show("Delete this script?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          script.Delete();
        }
        OnItemDeleted();
        Dispose();
      }
      return "";
    }
	}
}
